package com.maimao.goods.controller;

import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.Result;
import com.maimao.goods.controller.base.BaseController;
import com.maimao.goods.service.OrderService;
import com.maimao.model.order.confirm.CarIdsInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author MaoLin Wang
 * @date 2021/1/15 2:46 下午
 */
@RestController
@RequestMapping("/goods/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    /**
     * 购物车提交生成确认订单
     * TODO 如果是秒杀商品，则直接返回错误，不允许通过购物车秒杀
     * @return
     */
    @PostMapping("/confirm-order")
    public Result<?> confirmOrder(@RequestBody CarIdsInput carIdsInput) {

        if (carIdsInput == null || carIdsInput.getCarIds() == null || carIdsInput.getCarIds().size() <= 0) {
            return Result.notValid();
        }
        AuthUser authUser = checkAndGetAuthUser();
        List<Long> carIdLong = carIdsInput.getCarIds().stream().map(x -> Long.valueOf(x)).collect(Collectors.toList());
        return orderService.generateConfirmOrder(carIdLong, authUser.getId2Long());
    }
}
